package com.zimo.gui.order;

import com.zimo.download.info.ServerFileINFO;
import com.zimo.download.info.event.EventSet;
import com.zimo.download.info.event.EventSpeed;
import com.zimo.download.thread.ThreadManageDownload;
import com.zimo.gui.DownloadR;
import com.zimo.utils.StringFormat;
import java.util.ArrayList;


public class Lists implements Order{
    @Override
    public void handle(String[] p) {
        if (p.length >1){
            System.out.println("参数不合法，用输入 Help Lists查看使用");
            return;
        }
        DownloadR downloadR = DownloadR.getDownloadR();
        ArrayList<ThreadManageDownload> list = downloadR.list(Integer.valueOf(p[0]));
        for (ThreadManageDownload download : list) {
            try {
                System.out.println(download.getFileName()+"\t"+                                         /* 获取正在下载的文件名称 */
                        (ServerFileINFO.getFileSize(download.getUrl())/1024) +"kb"+"\t"+                /* 获取正在下载的文件大小(kb) */
                        download.getFinishThread()+"/"+download.getThreadCount()+"\t"+                  /* 获取线程完成比  下载完成的线程数/所有的线程数 */
                        (downloadR.getDownloadingFileSize(download)/1024)+"/"+(downloadR.getFileSize(download)/1024)+"kb"+"\t"+ /* 获取下载比 下载的大小/文件大小 */
                        ((double)download.getDownloadingFileSize()/(double) ServerFileINFO.getFileSize(download.getUrl())*100)+"%"  /* 获取下载进度 */
                        );
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private long speed;
    private long getSpeed(ThreadManageDownload download ){
        EventSet.getEventSet().addEventSpeed(download.getUrl(), new EventSpeed() {
            @Override
            public void handle(Long response, String threadID) {
                speed=response;
            }
        });
        return 0;
    }

    @Override
    public void describe() {
        System.out.println(StringFormat.formatLeftS("Lists",20)+"获取下载列表 [Lists state]");
    }

    @Override
    public void help() {
        describe();
        System.out.println("参数: ");
        System.out.println("\t"+StringFormat.formatLeftS("-1:",16)+"所有的下载任务 [Lists -1]");
        System.out.println("\t"+StringFormat.formatLeftS("0:",16)+"已经下载完成的任务 [Lists 0]");
        System.out.println("\t"+StringFormat.formatLeftS("1:",16)+"下载中的任务 [Lists 1]");
    }
}
